global class CreateResourceAllcationDetail implements Database.Batchable<sObject>,Database.Stateful
{
    string ActivityId;
    List<Activity_Competition_Brand_Information__c> CompetitionList;
    map<string,map<string,double>> kaCityList;//保存每个KA每个城市的target
    map<string,Integer> OtCityList;//保存OT每个城市的target
    map<string,List<CityResourceAllcation__c>> KAResourceList;//保存每个KA每个城市分配的数量
    map<string,List<CityResourceAllcation__c>> OTResourceList;//保存每个OT城市分配的数量
        
    global CreateResourceAllcationDetail(string activityId,List<Activity_Competition_Brand_Information__c> activityCompetitionList,
                                                            map<string,map<string,double>> KaCityList,map<string,Integer> OtCityList,
                                                             map<string,List<CityResourceAllcation__c>> KAResourceList,
                                                             map<string,List<CityResourceAllcation__c>> OTResourceList)
    {
        this.ActivityId=activityId;
        this.CompetitionList=activityCompetitionList;
        this.OtCityList=OtCityList;
        this.kaCityList=KaCityList;
        this.KAResourceList=KAResourceList;
        this.OTResourceList=OTResourceList;
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        string sql='select id,TargetNumber__c,OutletResourceType__c,KA__c,City__c,OwnerId from Participator_Detail_Outlet__c where ActivityId__c=\''+ActivityId+'\'';
        return Database.getQueryLocator(sql);
    }
    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        List<Resource_Allocation_Detail__c> OTNewList=new List<Resource_Allocation_Detail__c>();
        List<Resource_Allocation_Detail__c> KANewList=new List<Resource_Allocation_Detail__c>();
        
        for(SObject obj:scope)
        {
             Participator_Detail_Outlet__c pdo=(Participator_Detail_Outlet__c)obj;
             if(pdo.OutletResourceType__c=='OT')//处理OT的资源
             {
                 List<CityResourceAllcation__c> crlList=OTResourceList.get(pdo.City__c);
                 Integer CityTarget=OtCityList.get(pdo.City__c);
                 for(CityResourceAllcation__c cityRes:crlList)
                 {
                     double num=0;
                     if(pdo.TargetNumber__c!=NULL && pdo.TargetNumber__c!=0 && CityTarget!=NULL && CityTarget!=0)
                           num=(pdo.TargetNumber__c/CityTarget)*(cityRes.ResourceNumber__c*cityRes.PackSize__c);
                           
                     Resource_Allocation_Detail__c rad=new Resource_Allocation_Detail__c();
                     rad.Participator_Detail_Outlet__c=pdo.id;
                     rad.Resource_Allocation__c=cityRes.Resource_Allocation__c;
                     rad.Resource_Quantity_Number__c=num.round();
                     OTNewList.add(rad);
                 }
             }
             
             if(pdo.OutletResourceType__c=='KA')
             {
                List<CityResourceAllcation__c> crlList=KAResourceList.get(pdo.KA__c+pdo.City__c);//获取当前KA的城市的分配信息
                map<string,double> cityList=kaCityList.get(pdo.KA__c);//获取当前KA的城市target列表
                for(CityResourceAllcation__c cityRes:crlList)
                {
                    double num=0;
                     if(pdo.TargetNumber__c!=NULL && pdo.TargetNumber__c!=0 && cityList.get(pdo.City__c)!=NULL && cityList.get(pdo.City__c)!=0)
                           num=(pdo.TargetNumber__c/cityList.get(pdo.City__c))*(cityRes.ResourceNumber__c*cityRes.PackSize__c);
                           
                     Resource_Allocation_Detail__c rad=new Resource_Allocation_Detail__c();
                     rad.Participator_Detail_Outlet__c=pdo.id;
                     rad.Resource_Allocation__c=cityRes.Resource_Allocation__c;
                     rad.Resource_Quantity_Number__c=num.round();
                     KANewList.add(rad);
                }
             }
        }
        insert OTNewList;
        insert KANewList;
        
         //生成竞品信息
        List<Activity_Competition_Brand_Detail__c> competitionDetailList=new List<Activity_Competition_Brand_Detail__c>();
        for(Activity_Competition_Brand_Information__c competition: CompetitionList)
        {
             for(SObject obj:scope)
             {
                 Participator_Detail_Outlet__c pdo=(Participator_Detail_Outlet__c)obj;
                 if(pdo.OutletResourceType__c=='OT' )
                 {
                     Activity_Competition_Brand_Detail__c ac=new Activity_Competition_Brand_Detail__c();
                     ac.Activity_Competition_Brand_Information__c=competition.id;
                     ac.Participator_Detail_Outlet__c=pdo.id;
                     ac.OwnerId=pdo.OwnerId;
                     competitionDetailList.add(ac);
                 }
             }
        }
        insert competitionDetailList;
    } 
    global void finish(Database.BatchableContext BC)
    {
        //更新状态,标识已处理完
        Activity_Information__c ActivityInfo=[select IsCompleted__c from Activity_Information__c where id=:ActivityId];
        ActivityInfo.IsCompleted__c=true;
        update ActivityInfo;
    }
}